<template>
	<view>
		<view class="head">
			<view class="left" @click="gobilling"><image src="../../static/stock/left.png" mode=""></image></view>
			<view class="center">物流地址</view>
			<view class="right" @click="pushadress"><image src="../../static/bu/jia.png" mode=""></image></view>
		</view>
		<view class="box" @click="go">不需要发货地址</view>
		<view class="bad" v-for="(item, index) in address" :key="index" @click="gobilling2(index)">
			<view class="battop">{{ item }}</view>
			<view class="batbot">
				<view class="left" @click="del(index)">
					<image src="../../static/bu/delt.png" mode=""></image>
					删除
				</view>
				<view class="left" @click="change(index)">
					<image src="../../static/bu/gai.png" mode=""></image>
					修改
				</view>
			</view>
		</view>
		<uni-popup ref="popup" type="center">
			<view class="addContent">
				<view class="box">
					<view class="tit tCenter f36 c333">请输入物流地址</view>
					<view class="input-box"><input type="text" v-model="adre" /></view>
					<view class="btn f30 tCenter">
						<view class="dropBtn" @click="close">取消</view>
						<view class="submit" @click="open">确定</view>
					</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
import uniPopup from '@/components/uni-popup/uni-popup.vue';
export default {
	data() {
		return {
			add: {},
			address: [],
			name: '',
			customer_id: '',
			adre: '',
			telephone: '',
			eindex: -1
		};
	},
	components: {
		uniPopup
	},
	onLoad(e) {
		this.add = JSON.parse(e.adress);
        console.log(this.add);
		if(this.add.address==''){
			this.address=[]
			this.name = this.add.name;
			this.customer_id = this.add.customer_id;
			this.telephone = this.add.telephone;
			return
		}
		this.address = JSON.parse(this.add.address);

		 
		this.name = this.add.name;
		this.customer_id = this.add.customer_id;
		this.telephone = this.add.telephone;
	},
	methods: {
		gobilling() {
			uni.navigateBack({
				delta:1
			})
		},
		close() {
			this.$refs.popup.close();
		},
		open() {
			if (this.eindex != -1) {
				this.address[this.eindex] = this.adre;
				this.$set(this.address, this.eindex, this.address[this.eindex]);
			} else {
				this.address.push(this.adre);
			}
			this.$refs.popup.close();
			this.$ajax('/api/v1.customer/edit', {
				customer_id: this.customer_id,
				address: JSON.stringify(this.address),
				name: this.name,
				telephone: this.telephone
			})
				.then(res => {
					// console.log(JSON.parse(res.result.address) );
					this.address = JSON.parse(res.result.address);
					this.add = res.result;
					console.log(this.add);
				})
				.catch(res => {
					console.log(Error);
				});
		},
		pushadress() {
			this.$refs.popup.open();
			this.eindex = -1;
		},
		del(e) {
			// console.log(e);
			this.address.splice(e, 1);
		},
		change(e) {
			this.$refs.popup.open();
			this.eindex = e;
			this.adre = this.address[e];
		},
		gobilling2(e) {
		
			uni.navigateTo({
				url: '../order/billing?item=' + JSON.stringify(this.add) + '&index=' +JSON.stringify(e)
			});
		},
		go() {
			uni.navigateBack({		
		delta:1	
			});
		}
	}
};
</script>

<style lang="less">
page {
	background-color: #f7f4f8;
}

.head {
	height: 88rpx;
	display: flex;
	justify-content: space-between;
	background-color: #249af6;
	color: #ffffff;
	padding: 27rpx 30rpx;
	box-sizing: border-box;
	.left {
		image {
			width: 22rpx;
			height: 39rpx;
		}
	}
	.center {
		font-size: 36rpx;
		font-family: PingFang SC;
		font-weight: 500;
	}
	.right {
		image {
			width: 44rpx;
			height: 44rpx;
		}
	}
}
.box {
	height: 89rpx;
	line-height: 89rpx;
	font-size: 30rpx;
	font-family: PingFang SC;
	font-weight: 500;
	color: rgba(102, 102, 102, 1);
	padding-left: 30rpx;
}
.bad {
	margin-bottom: 20rpx;
	background-color: #ffffff;
	padding: 30rpx;
	box-sizing: border-box;
	.battop {
		height: 145rpx;
		font-size: 30rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: rgba(51, 51, 51, 1);
		border-bottom: 3rpx solid #eeeeee;
	}
	.batbot {
		display: flex;
		justify-content: flex-end;
		height: 98rpx;
		.left {
			margin-left: 70rpx;
			line-height: 98rpx;
			font-size: 30rpx;
			font-family: PingFang SC;
			font-weight: 500;
			color: rgba(102, 102, 102, 1);
			image {
				margin-left: 20rpx;
				margin-right: 5rpx;
				width: 30rpx;
				height: 30rpx;
			}
		}
	}
}
.addContent {
	height: 330rpx;
}
// 选择弹出
.chooseBox {
	width: 100%;
	padding-top: 30upx;
	.choose-item {
		width: 100%;
		height: 100%;
		display: flex;
		flex: 1;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;
		padding: 0 30upx 35upx 30upx;
		margin-bottom: 35upx;
		border-bottom: 2upx #eee solid;
		box-sizing: border-box;
		.arrow {
			image {
				width: 20upx;
				height: 35upx;
			}
		}
	}
}
</style>
